/**
 *	TrueMusic - simple Java music player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import java.applet.*;
import java.io.*;

/**
 * Audio implemented with java.applet.AudioClip, WARNING: Does not completely implement all functions expected from an Audio.
 */
public class AppletClipAudio implements Audio
{
	private AudioClip a;
	private String res;

	private boolean playing = false;

	public void load(String res, PlaybackListener l) throws Exception
	{
		this.res = res;

		a = Applet.newAudioClip(new File(res).toURL());
		a = null;
	}

	public void play()
	{
		if(a==null) try{ a = Applet.newAudioClip(new File(res).toURL()); }catch(Exception e){}

		new Thread()
		{
			public void run()
			{
				playing = true;
				a.play();
				playing = false;
			}
		}.start();
	}

	public void pause()
	{
		stop();
	}

	public void stop()
	{
		new Thread()
		{
			public void run()
			{
				a.stop();
				playing = false;
			}
		}.start();
	}

	public boolean isPlaying()
	{
		return playing;
	}

	public String getTitle()
	{
		return new File(res).getName();
	}

	public String getArtist()
	{
		return "";
	}

	public String getAlbum()
	{
		return "";
	}

	public String getTrack()
	{
		return "";
	}

	public String getGenre()
	{
		return "";
	}

	public String getLyrics()
	{
		return "";
	}

	public String toString()
	{
		return res;
	}

	public int getFrames()
	{
		return 0;
	}

	/**
	 * Ignored
	 */
	public void seek(int frame)
	{
	}

	public String getResource()
	{
		return res;
	}

	public String formatName()
	{
		return "Aiff/Au";
	}

	public String[] getExtensions()
	{
		return new String[]{".au",".aif",".aiff"};
	}
}
